<?php use model\userModel;
    use Model\eventsModel;
    use Model\orderModel;
    use Controller\Controller;

    class ordersController extends Controller{
        private $EVENT;
        private $ADMIN;
        private $ORDER;
        private $USER;

        public function __construct() {
            session_start();
            $this->EVENT = new eventsModel();
            $this->USER = new userModel();
            $this->ORDER = new orderModel();
        }

        public function getAction($params = null) {
            $response = [];
            if ($this->request()["method"] !== "GET") {
                $this->errorJsonDie(400);
            }
            if ($params === null) {
                $response = $this->EVENT->getAllEvents();
            } else {
                $response = $this->EVENT->getEventById(((int) $params));
            }
            if ($response["status"] == 0){
                $this->errorJsonDie(400, "Not found");
            }
            $this->successJson($response["response"]);
        }
        public function addAction() {
            $request = $this->request();
            $json = $this->request()["json"];
            $response = [];

            if ($this->request()["method"] !== "POST") {
                $this->errorJsonDie(404);
            }

            if (!isset($json["user_id"])
                || !isset($json["event_id"])
                || !isset($json["token"])
            ){
                $this->errorJsonDie(400, "mismatch params");
            }

            $response = $this->USER->checkUserToken($json['token']);
            if ($response['status'] == 0){
                $this->errorJsonDie(403, "unauthorized");
            }

            $response = $this->ORDER->addOrder($json['user_id'], $json['event_id']);
            if ($response['status'] == 0){
                $this->errorJsonDie(400);
            }
            $this->successJson($response["response"]);
        }
        public function editAction() {
            $this->ADMIN = (isset($_SESSION['admin'])) ? $this->ADMIN = $_SESSION['admin'] : $this->errorJsonDie(403, "unauthorized");
            if ($this->request()["method"] !== "PATCH") {
                $this->errorJsonDie(400);
            }

        }

        public function deleteAction($param = null) {
            $request = $this->request();
            $json = $request['json'];
            if ($this->request()["method"] !== "DELETE") {
                $this->errorJsonDie(400, "WTF");
            }
            if (!isset($json["user_id"])
                || !isset($json["event_id"])
                || !isset($json["token"])
            ){
                $this->errorJsonDie(400, "mismatch params");
            }
            $response = $this->USER->checkUserToken($json['token']);
            if ($response['status'] == 0){
                $this->errorJsonDie(403, "unauthorized");
            }
            $response = $this->ORDER->deleteOrder($json["user_id"], $json["event_id"]);
            if ($response["status"] === 0) {
                $this->errorJsonDie(404, "Not found");
            }
            $this->successJson("Delete");
        }
    }